package com.hjc.controller;

import com.alibaba.excel.EasyExcel;
import com.hjc.entity.User;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author hjc-YYDS
 * @date 2024/3/5 10:34
 */
@Slf4j
@RestController
@RequestMapping("/home")
public class HomeController {

    @RequestMapping("/index")
    public String index() {
        return "Hello World!";
    }

    @SneakyThrows
    @PostMapping("/upload")
    public List<User> upload(MultipartFile file) {
        InputStream inputStream = file.getInputStream();

        List<User> list = EasyExcel.read(inputStream).sheet().head(User.class).doReadSync();

       log.info(list.toString());

       return list;
    }

    @SneakyThrows
    @PostMapping("/upload/file")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            long fileSize = file.getSize();
            InputStream inputStream = file.getInputStream();

            // 进行文件处理操作
            String st = "fileName:" + fileName + ", fileSize: " + fileSize;
            return st;
        } else {
            return "No file uploaded!";
        }
    }
}
